﻿using Microsoft.AspNetCore.DataProtection;

namespace WebAPI.Common
{
    public class DataProtectorUtility: IDataProtectorUtility
    {
        IDataProtector _protector;

        // the 'provider' parameter is provided by DI
        public DataProtectorUtility(IDataProtectionProvider provider)
        {
            _protector = provider.CreateProtector("IHAP Data Protect");
        }

        public string Protect(string input)
        {
            // protect the payload
            string protectedPayload = _protector.Protect(input);
#if DEBUG
            Console.WriteLine($"Protect returned: {protectedPayload}");
#endif
            return protectedPayload;
        }

        public string UnProtect(string protectedPayload)
        {
            // unprotect the payload
            string unprotectedPayload = _protector.Unprotect(protectedPayload);
#if DEBUG
            Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
#endif
            return unprotectedPayload;
        }
    }
}
